package org.jeecg.modules.ysl.college.service.impl;

import org.jeecg.modules.ysl.college.entity.College;
import org.jeecg.modules.ysl.college.entity.CeeEnrollPlan;
import org.jeecg.modules.ysl.college.mapper.CeeEnrollPlanMapper;
import org.jeecg.modules.ysl.college.mapper.CollegeMapper;
import org.jeecg.modules.ysl.college.service.ICollegeService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 院校
 * @Author: jeecg-boot
 * @Date:   2021-07-19
 * @Version: V1.0
 */
@Service
public class CollegeServiceImpl extends ServiceImpl<CollegeMapper, College> implements ICollegeService {

	@Autowired
	private CollegeMapper collegeMapper;
	@Autowired
	private CeeEnrollPlanMapper ceeEnrollPlanMapper;
	
	@Override
	@Transactional
	public void saveMain(College college, List<CeeEnrollPlan> ceeEnrollPlanList) {
		collegeMapper.insert(college);
		if(ceeEnrollPlanList!=null && ceeEnrollPlanList.size()>0) {
			for(CeeEnrollPlan entity:ceeEnrollPlanList) {
				//外键设置
				entity.setCollegeId(college.getId());
				ceeEnrollPlanMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(College college,List<CeeEnrollPlan> ceeEnrollPlanList) {
		collegeMapper.updateById(college);
		
		//1.先删除子表数据
		ceeEnrollPlanMapper.deleteByMainId(college.getId());
		
		//2.子表数据重新插入
		if(ceeEnrollPlanList!=null && ceeEnrollPlanList.size()>0) {
			for(CeeEnrollPlan entity:ceeEnrollPlanList) {
				//外键设置
				entity.setCollegeId(college.getId());
				ceeEnrollPlanMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		ceeEnrollPlanMapper.deleteByMainId(id);
		collegeMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			ceeEnrollPlanMapper.deleteByMainId(id.toString());
			collegeMapper.deleteById(id);
		}
	}
	
}
